package com.yuke.cloud.service.cmc.controller.frontend;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuke.cloud.common.base.dto.LoginAuthDto;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.cmc.entity.CmsAppPage;
import com.yuke.cloud.service.cmc.entity.CmsAppStartPage;
import com.yuke.cloud.service.cmc.entity.CmsHotWord;
import com.yuke.cloud.service.cmc.service.CmsAppStartPageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wg on 2019/01/18.
 */
@Api(value = "WEB - CmsAppStartPageController", tags = "处理CmsAppStartPage相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/app_start_page")
public class CmsAppStartPageController extends BaseController {
    @Resource
    private CmsAppStartPageService cmsAppStartPageService;
    @Autowired
    private OpcFileFeignApi opcFileFeignApi;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "cmsAppStartPage", value = "该数据记录的POJO") @RequestBody CmsAppStartPage cmsAppStartPage) {
        cmsAppStartPageService.save(cmsAppStartPage);
        return WrapMapper.ok(cmsAppStartPage.getStartPageId());
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id,
                          @RequestParam(value = "dataUrl",required = false)String dataUrl) {
        cmsAppStartPageService.deleteById(id);
        if (dataUrl!=null&&!dataUrl.equals("")) {
            List<UploadFile> uploadFiles = new ArrayList<>();
            UploadFile uploadFile = new UploadFile();
            uploadFile.setFilePath(dataUrl);
            uploadFiles.add(uploadFile);
            opcFileFeignApi.fileDelete(uploadFiles);
        }
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "cmsAppStartPage", value = "需要修改的内容") @RequestBody CmsAppStartPage cmsAppStartPage) {
        cmsAppStartPageService.update(cmsAppStartPage);
        String oldDataUrl = cmsAppStartPage.getOldDataUrl();
        if (oldDataUrl != null && !oldDataUrl.equals("null") && !oldDataUrl.equals("undefined") && !oldDataUrl.equals("")) {
            List<UploadFile> uploadFiles = new ArrayList<>();
            UploadFile uploadFile = new UploadFile();
            uploadFile.setFilePath(cmsAppStartPage.getOldDataUrl());
            uploadFiles.add(uploadFile);
            opcFileFeignApi.fileDelete(uploadFiles);
        }
        return WrapMapper.ok();
    }

    @PostMapping("/batchSave")
    @ApiOperation(httpMethod = "POST", value = "批量修改")
    public Wrapper batchSave(@RequestBody List<CmsAppStartPage> cmsHotWords) {
        for (CmsAppStartPage cmsHotWord : cmsHotWords) {
            LoginAuthDto loginAuthDto = getLoginAuthDto();
            cmsHotWord.setUpdateUser(loginAuthDto.getUserId());
            cmsAppStartPageService.update(cmsHotWord);
        }
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsAppStartPage> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsAppStartPage cmsAppStartPage = cmsAppStartPageService.findById(id);
        return WrapMapper.ok(cmsAppStartPage);
    }

    //批量删除
    @PostMapping("/batchDelete")
    @ApiOperation(httpMethod = "POST", value = "批量删除")
    public Wrapper batchDelete(@RequestBody JSONArray jsonArray) {
        List<UploadFile> uploadFiles = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            UploadFile uploadFile = new UploadFile();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Long startPageId = Long.valueOf(jsonObject.get("startPageId").toString());
            cmsAppStartPageService.deleteById(startPageId);
            Object dataType = jsonObject.get("dataType");
            if (dataType != null && dataType.toString().equals("1")) {
                String dataUrl = jsonObject.get("dataUrl") + "";
                uploadFile.setFilePath(dataUrl);
                uploadFiles.add(uploadFile);
            }
        }
        if (uploadFiles.size() > 0) {
            opcFileFeignApi.fileDelete(uploadFiles);
        }

        return WrapMapper.ok();
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<CmsAppStartPage>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                   @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<CmsAppStartPage> list = cmsAppStartPageService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/ifUnique")
    @ApiOperation(httpMethod = "GET", value = "唯一性判断")
    public Wrapper ifUnique(@RequestParam("pageName") String pageName,
                            @RequestParam(value = "startPageId", required = false) Long startPageId
    ) {
        Example example = new Example(CmsAppStartPage.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pageName", pageName);
        if (startPageId != null && !startPageId.toString().equals("")) {
            criteria.andNotEqualTo("startPageId", startPageId);
        }
        List<CmsAppStartPage> list = cmsAppStartPageService.selectByExample(example);
        if (list.size() == 0) {
            return WrapMapper.ok(true);
        } else {
            return WrapMapper.ok(false);
        }

    }


}
